package top.easyblog.core.mq;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.easyblog.core.util.AssertUtil;

/**
 * Redis消息生产者
 *
 * @author: frank.huang
 * @date: 2023-12-28 21:53
 */
@Slf4j
@Component
public class RedisMessageProducer {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${spring.profiles.active}")
    private String env;

    /**
     * 向Redis push表数据，
     *
     * @param msg   消息
     * @param topic 主题
     * @throws Exception 异常
     */
    public void sendRedisMessage(String msg, RedisMessageTopic topic) {
        AssertUtil.notNull(topic, "Kafka topic 不能为空");
        try {
            String fullTopic = String.format(topic.getTopic(), env);
            redisTemplate.convertAndSend(fullTopic, msg);
            log.info("Send message to topic [{}]: {}", fullTopic, msg);
        } catch (Exception e) {
            log.error("sendMsg to kafka failed!", e);
        }
    }
}
